import me.w70.bot.Bot;
import me.w70.bot.scripting.Random;
import me.w70.bot.wrappers.RSInterface;
import me.w70.bot.wrappers.RSItemTile;
import me.w70.bot.wrappers.RSNPC;
import me.w70.bot.wrappers.RSObject;
import me.w70.hooks.Tile;


public class Forester extends Random {

	private RSNPC forester;
	private final int ForesterID = 2458;
	private final int searchInter = 242;
	final Tile walkTo = new Tile(2610, 4775);

	int phe = -1;

	boolean done = false;

	@Override
	public boolean canRun() {
		if (!isLoggedIn()) {
			return false;
		}

		forester = getNearestNPCByID(ForesterID);
		if (forester != null) {
			wait(random(2000, 3000));
			return true;
		}
		return false;
	}

	@Override
	public String getScriptAuthor() {
		return "spooky";
	}

	@Override
	public String getScriptName() {
		return "Freaky Forester";
	}
	public int getForesterState() {
		if(done) {
			return 3;
		} else if(canContinue()) {
			return 1;
		} else if(phe == -1) {
			return 0;
		} else if(inventoryContains(6178)) {
			return 0;
		} else if(phe != -1) {
			return 2;
		} else {
			return 0;
		}
	}


	@Override
	public int loop() {
		forester = getNearestNPCByID(ForesterID);
		if (forester == null) {
			return -1;
		}

		if (getMyPlayer().getAnimation() != -1) {
			return random(3000, 5000);
		} else if(getMyPlayer().isMoving()) {
			return random(200, 500);
		}

		if (done != true) {
			done = searchText(241, "Thank you")
			|| getInterface(242, 4).containsText("leave");
		}

		if (inventoryContains(6179)) {
			phe = -1;
			atInventoryItem(6179, "rop");
			return random(500, 900);
		}

		if (getInventoryCount(false) == 28 && !inventoryContains(6178)) {
			Bot.addMessage(this, "Inventory is full. Closing all scripts & logging out.");
			Bot.stopScripts();
			logout();
		}

		switch(getForesterState()) {
		case 0:  // Talk to forester
			if(tileOnScreen(forester.getLocation()) && distanceTo(forester.getLocation()) <= 5) {
				atNPC(forester, "Talk");
			} else if (distanceTo(forester.getLocation()) >= 5) {
				walkTile(randomizeTile(forester.getLocation(), 3, 3));
				turnToTile(randomizeTile(forester.getLocation(), 3, 3));
			}
			return random(500, 800);
		case 1:  // Talking
			if (searchText(searchInter, "one-")) {
				phe = 2459;
			} else if (searchText(searchInter, "two-")) {
				phe = 2460;
			} else if (searchText(searchInter, "three-")) {
				phe = 2461;
			} if (searchText(searchInter, "four-")) {
				phe = 2462;
			}
			if(phe != -1) {
				Bot.addMessage(this, "Pheasant ID: " + phe);
			}
			if (myClickContinue()) {
				return random(500, 800);
			}
			return random(200, 500);
		case 2:  // Kill pheasant
			if(phe == -1) {
				return random(200, 500);
			}
			final RSNPC Pheasant = getNearestFreeNPCByID(phe);
			final RSItemTile tile = getGroundItemByID(6178);
			if (tile != null) {
				atTile(tile, "Take");
				return random(600, 900);
			} else if(Pheasant != null){
				if(tileOnScreen(Pheasant.getLocation()) && distanceTo(Pheasant.getLocation()) <= 5) {
					atNPC(Pheasant, "ttack");
					return random(1000, 1500);
				} else if (distanceTo(Pheasant.getLocation()) >= 5) {
					walkTile(randomizeTile(Pheasant.getLocation(), 3, 3));
					turnToTile(randomizeTile(Pheasant.getLocation(), 3, 3));
				}
			} else {
				return random(2000, 5000);
			}
		case 3:  // Get out
			if (!tileOnScreen(walkTo)) {
				if(tileOnMap(walkTo)) {
					walkTile(walkTo);
				} else {
					walkTile(randomizeTile(forester.getLocation(), 5, 5));
				}
				return random(900, 1200);
			}

			final RSObject Portal = findObject(8972);

			if (Portal == null) {
				Bot.addMessage(this, "Could not find portal.");
				return random(800, 1200);
			}

			if (atObject(Portal, "Enter")) {
				return random(800, 1200);
			}
			return random(200, 500);
		}
		return random(1000, 1500);
	}

	public boolean myClickContinue() {
		wait(random(800, 1000));
		return atInterface(243, 7) || atInterface(241, 5)
		|| atInterface(242, 6) || atInterface(244, 8)
		|| atInterface(64, 5);
	}

	public boolean searchText(final int interfac, final String text) {
		final RSInterface talkFace = getInterface(interfac,0);
		if (!talkFace.isValid()) {
			return false;
		}
		for (int i = 0; i < talkFace.getChildren().length; i++) {
			if (talkFace.getChild(i).containsText(text)) {
				return true;
			}
		}

		return false;
	}

	@Override
	public String getScriptCategory() {
		// TODO Auto-generated method stub
		return "Random";
	}

	@Override
	public String getScriptDescription() {
		// TODO Auto-generated method stub
		return "Solves the Freaky Forester Random";
	}
}